<?php
namespace Home\Controller;
use Think\Controller;
class OrderController extends CommonController {

    protected $orderModel;
    public function _initialize(){
        parent::_initialize();
        $this->orderModel = D("order");
    }
    //查询订单列表
    public function index(){
        $post = I("");
        $keyword = trim($post['keyword']);
        $start_time = $post['start_time'];
        $end_time = $post['end_time'];

        $this->assign('keyword',$keyword);
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        if($start_time){
            $s_time = strtotime($start_time);
            $cond['ecm_order.add_time'] = array("egt",$s_time);
            if($end_time){
                $e_time = strtotime($end_time);
                $cond['ecm_order.add_time'] = array("between",array($s_time,$e_time));
            }
        }else if($end_time){
            $e_time = strtotime($end_time);
            $cond['ecm_order.add_time'] = array("elt",$e_time);
        }
        if($keyword){
            $cond['ecm_member.s_name|ecm_member.user_name|ecm_member.phone_mob|ecm_order.order_sn|ecm_order_goods.goods_name|ecm_order.seller_name'] = array('like',"%$keyword%");
            $this->assign('keyword',$keyword);
        }
        $cond['ecm_order.status'] = array('not in','0,10,11,50');
        $cond['ecm_order.seller_id'] = $this->user_id;
        $con = array(
            'condition' => $cond,
            'field' => 'ecm_order.* ,ecm_order_goods.specification, ecm_order_goods.price, ecm_order_goods.tax_price, ecm_order_goods.goods_name,ecm_member.*',
            'order' => 'order_id desc'
        );
        $count = $this->orderModel->getOrderList($con);// 查询满足要求的总记录数
        $page = I('p') ? I('p') : '1';
        $pageSize = I('pageSize') ? I('pageSize') : '15';
        $con['page'] = $page.','.$pageSize;
        $res = $this->orderModel->getOrderList($con);
        $map = array(
            'start_time' => $start_time,
            'end_time'   => $end_time,
            'keyword'    => $keyword
        );
        $show = $this->getPage(count($count),$page,$pageSize,$map);
        $this->assign('order',$res);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }
    //导出功能
    public function excel(){
        if($_GET['order_id']){
            $where['ecm_order.order_id'] = $_GET['order_id'];
        }else{
            $post = I("");
            $keyword    = trim($post['keyword']);
            $start_time = $post['start_time'];
            $end_time   = $post['end_time'];

            if($start_time){
                $s_time = strtotime($start_time);
                $where['ecm_order.add_time'] = array("egt",$s_time);
                if($end_time){
                    $e_time = strtotime($end_time);
                    $where['ecm_order.add_time'] = array("between",array($s_time,$e_time));
                }
            }else if($end_time){
                $e_time = strtotime($end_time);
                $where['ecm_order.add_time'] = array("elt",$e_time);
            }
            if($keyword){
                $where['ecm_member.s_name|ecm_member.user_name|ecm_member.phone_mob|ecm_order.order_sn|ecm_order_goods.goods_name|ecm_order.seller_name'] = array('like',"%$keyword%");
            }
        }
        $where['ecm_order.status'] = array('not in','0,10,11,50');
        $where['ecm_order.seller_id'] = $this->user_id;
        $where = array(
            'condition' => $where,
            'field' => 'ecm_order.* ,ecm_order_goods.specification, ecm_order_goods.price, ecm_order_goods.tax_price, ecm_order_goods.goods_name,ecm_member.*',
            'order' => 'order_id desc'
        );
        $info = $this->orderModel->getOrderList($where);// 查询满足条件的订单

        require '../sellerserver/ThinkPHP/Library/Vendor/PHPExcel/PHPExcel.php';
        $excel = new \PHPExcel();
        $objActSheet = $excel->getActiveSheet();
        $objActSheet->setCellValue('A1', '用户名');
        $objActSheet->setCellValue('B1', '订单号');
        $objActSheet->setCellValue('C1', '商家名称');
        $objActSheet->setCellValue('D1', '商品名称');
        $objActSheet->setCellValue('E1', '商品单价');
        $objActSheet->setCellValue('F1', '商品税费');
        $objActSheet->setCellValue('G1', '规格');
        $objActSheet->setCellValue('H1', '折扣价');
        $objActSheet->setCellValue('I1', '订单折扣价');
        $objActSheet->setCellValue('J1', '合计金额');
        $objActSheet->setCellValue('K1', '订单状态');
        $objActSheet->setCellValue('L1', '下单时间');
        $objActSheet->setCellValue('M1', '归属店铺');

        $num = 0;
        foreach ($info as $k => $v){
            switch ($v['status']){
                case 20:
                    $status = '待发货';
                    break;
                case 30:
                    $status = '已发货';
                    break;
                case 40:
                    $status = '已完成';
                    break;
            }


            $num += 2;
            $k +=2;
            $time = date('Y-m-d H:i:s',$v['add_time']);

            $objActSheet->setCellValue('A'.$k, $v['buyer_name']);
            $objActSheet->setCellValue('B'.$k, $v['order_sn']);
            $objActSheet->setCellValue('C'.$k, $v['seller_name']);
            $objActSheet->setCellValue('D'.$k, $v['goods_name']);
            $objActSheet->setCellValue('E'.$k, $v['price']);
            $objActSheet->setCellValue('F'.$k, $v['tax_price']);
            $objActSheet->setCellValue('G'.$k, $v['specification']);
            $objActSheet->setCellValue('H'.$k, $v['discount']);
            $objActSheet->setCellValue('I'.$k, $v['order_amount']);
            $objActSheet->setCellValue('J'.$k, $v['goods_amount']);
            $objActSheet->setCellValue('K'.$k, $status);
            $objActSheet->setCellValue('L'.$k, $time);
            $objActSheet->setCellValue('M'.$k, $v['s_name']);
        }

        $excel = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');  //Excel2007  兼容好
        ob_end_clean();   //解决乱码
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename=订单'.date('Y-m-d H:i:s'). '.xlsx');
        header('Cache-Control: max-age=0');
        $excel->save('php://output');
        exit;

    }

}